/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test;

import java.util.List;

import junit.framework.AssertionFailedError;

import org.junit.Test;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.annotations.Attribute;
import org.sourceprojects.lycia.annotations.Child;
import org.sourceprojects.lycia.annotations.Tag;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.fluent.FluentBuilder;
import org.w3c.dom.Element;

public class AnnotatedChildListTestCase {
	private static final String XML_DATA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<matches><set id=\"test\"><file name=\"file1\" /><file name=\"file2\"><file name=\"file4\" /></file><file name=\"file3\"><file name=\"file5\" />file5_inner</file></set></matches>";

	@Test
	public void testParsingChildListWithListtypeElement()
			throws LyciaParsingException {

		FluentBuilder<Object> builder = FluentBuilder.prepare();
		LyciaParser<Object> parser = builder
				.parser(
						FluentBuilder.tag("set", FluentBuilder.method(
								"parseSetElementList", this, String.class,
								List.class))).build();

		parser.parse(XML_DATA);
	}

	@Test
	public void testParsingChildListWithListtypeString()
			throws LyciaParsingException {

		FluentBuilder<Object> builder = FluentBuilder.prepare();
		LyciaParser<Object> parser = builder.parser(
				FluentBuilder.tag("set", FluentBuilder.method(
						"parseSetStringList", this, String.class, List.class)))
				.build();

		parser.parse(XML_DATA);
	}

	public void parseSetElementList(@Attribute("id") String id,
			@Child(@Tag("file\\file")) List<Element> elements) {

		if (elements.size() != 2)
			throw new AssertionFailedError("Size of 2 expected but was "
					+ elements.size());

		if (!(elements.get(0) instanceof Element))
			throw new AssertionFailedError(
					"First element is not of type org.w3c.dom.Element");
	}

	public void parseSetStringList(
			@Attribute("id") String id,
			@Child(value = @Tag("file\\file"), listType = String.class) List<String> elements) {

		if (elements.size() != 2)
			throw new AssertionFailedError("Size of 2 expected but was "
					+ elements.size());

		if (!(elements.get(0) instanceof String))
			throw new AssertionFailedError(
					"First element is not of type java.lang.String");
	}

}
